package school.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import school.domain.Player;
import school.domain.Team;
import school.mapper.PlayerMapper;
import school.mapper.TeamMapper;
import school.support.BaseController;
import school.support.util.ParamUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Controller
@RequestMapping("player")
public class PlayerController extends BaseController {

    @Autowired
    private PlayerMapper playerMapper;
    @Autowired
    private TeamMapper teamMapper;

    /**
     * 球员列表
     *
     * @param player
     * @param pageNum
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(Player player, @RequestParam(defaultValue = "1") Integer pageNum, Model model) {
        PageHelper.startPage(pageNum, 10);
        Example example = new Example(Player.class);
        if (player.getName() != null)
            example.createCriteria()
                    .andCondition("p.name like '%" + player.getName() + "%'");
        PageInfo<Player> pageInfo = new PageInfo<>(playerMapper.selectModelByExample(example), 5);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("requestUrl", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "player/list";
    }

    /**
     * 删除球员
     *
     * @param id
     * @return
     */
    @RequestMapping("del/{id}")
    public String del(@PathVariable Integer id) {
        playerMapper.deleteByPrimaryKey(id);
        return refresh();
    }

    /**
     * 添加球员
     *
     * @return
     */
    @GetMapping("add")
    public String add(Model model) {
        List<Team> teamList = teamMapper.selectAll();
        model.addAttribute("teamList", teamList);
        return "player/add";
    }

    /**
     * 添加球员
     *
     * @return
     */
    @PostMapping("add")
    public String add(Player player) {
        playerMapper.insertSelective(player);
        return "redirect:list.do";
    }

    /**
     * 球员详情
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        List<Team> teamList = teamMapper.selectAll();
        Player player = playerMapper.selectByPrimaryKey(id);
        model.addAttribute("player", player);
        model.addAttribute("teamList", teamList);
        return "player/detail";
    }

    /**
     * 更新球员信息
     *
     * @param player
     * @return
     */
    @PostMapping("update")
    public String update(Player player) {
        playerMapper.updateByPrimaryKeySelective(player);
        return "redirect:list.do";
    }

    @RequestMapping("list-query")
    public String query(Player player, @RequestParam(defaultValue = "1") Integer pageNum, Model model) {
        PageHelper.startPage(pageNum, 10);
        Example example = new Example(Player.class);
        if (player.getName() != null)
            example.createCriteria()
                    .andLike("name", "%" + player.getName() + "%");
        PageInfo<Player> pageInfo = new PageInfo<>(playerMapper.selectModelByExample(example), 5);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("requestUrl", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "player/query";
    }

}
